<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/shipping.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <table class="form" id="module0">
        	<tr>
          	<th colspan='6' style="text-align:left; font-size:16px"><?php echo $this->config->get('config_name'); ?></th>
          </tr>
          <tr>
            <td><?php echo $entry_cost; ?></td>
            <td><?php echo $entry_tax_class; ?></td>
            <td><?php echo $entry_geo_zone; ?></td>
            <td><?php echo $entry_status; ?></td>
            <td><?php echo $entry_sort_order; ?></td>
          </tr>
          <?php $module_rows = array();
          $module_rows[0] = 0;
          $modules = explode(':', $multiflat_cost);
          $modules_count = count($modules);
          if($modules_count > 0) {
          	$multiflat_cost = explode(':', $multiflat_cost);
          	$multiflat_tax_class_id = explode(':', $multiflat_tax_class_id);
          	$multiflat_geo_zone_id = explode(':', $multiflat_geo_zone_id);
          	$multiflat_status = explode(':', $multiflat_status);
          	$multiflat_sort_order = explode(':', $multiflat_sort_order);
          } ?>
          <?php for ($i = 0; $i < $modules_count; $i++) { ?>
          <tbody id="module-row<?php echo '0_' . $module_rows[0]; ?>">
            <tr>
              <td><input type="text" name="multiflat_cost_0_<?php echo $module_rows[0]; ?>" value="<?php echo $multiflat_cost[$i]; ?>" /></td>
              <td><select name="multiflat_tax_class_id_0_<?php echo $module_rows[0]; ?>">
                    <option value="0"><?php echo $text_none; ?></option>
                    <?php foreach ($tax_classes as $tax_class) { ?>
                    <?php if ($tax_class['tax_class_id'] == $multiflat_tax_class_id[$i]) { ?>
                    <option value="<?php echo $tax_class['tax_class_id']; ?>" selected="selected"><?php echo $tax_class['title']; ?></option>
                    <?php } else { ?>
                    <option value="<?php echo $tax_class['tax_class_id']; ?>"><?php echo $tax_class['title']; ?></option>
                    <?php } ?>
                    <?php } ?>
                  </select></td>
              <td><select name="multiflat_geo_zone_id_0_<?php echo $module_rows[0]; ?>">
                  <option value="0"><?php echo $text_all_zones; ?></option>
                  <?php foreach ($geo_zones as $geo_zone) { ?>
                  <?php if ($geo_zone['geo_zone_id'] == $multiflat_geo_zone_id[$i]) { ?>
                  <option value="<?php echo $geo_zone['geo_zone_id']; ?>" selected="selected"><?php echo $geo_zone['name']; ?></option>
                  <?php } else { ?>
                  <option value="<?php echo $geo_zone['geo_zone_id']; ?>"><?php echo $geo_zone['name']; ?></option>
                  <?php } ?>
                  <?php } ?>
                </select></td>
              <td><select name="multiflat_status_0_<?php echo $module_rows[0]; ?>">
                  <?php if ($multiflat_status[$i]) { ?>
                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                  <option value="0"><?php echo $text_disabled; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_enabled; ?></option>
                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                  <?php } ?>
                </select></td>
              <td><input type="text" name="multiflat_sort_order_0_<?php echo $module_rows[0]; ?>" value="<?php echo !empty($multiflat_sort_order[$i]) ? $multiflat_sort_order[$i] : '0'; ?>" size="1" /></td>
              <td class="right"><a onclick="$('#module-row<?php echo '0_' . $i; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
            </tr>
          </tbody>
          <?php $module_rows[0]++; ?>
          <?php } ?>
          <tfoot>
            <tr>
              <td colspan="6"></td>
              <td class="right"><a onclick="addModule(0);" class="button"><?php echo $button_add_module; ?></a>
         		 	<input type="hidden" name="multiflat_0" id="multiflat_0" value="<?php echo $module_rows[0]; ?>" /></td>
            </tr>
          </tfoot>
        </table>
        <?php
        $store_multiflat_total = 0;
        foreach ($stores as $store) {
        ?>
        <table class="form" id="module<?php echo $store['store_id']; ?>">
        	<tr>
          	<th colspan='6' style="text-align:left; font-size:16px"><?php echo $store['name']; ?></th>
          </tr>
          <tr>
            <td><?php echo $entry_cost; ?></td>
            <td><?php echo $entry_tax_class; ?></td>
            <td><?php echo $entry_geo_zone; ?></td>
            <td><?php echo $entry_status; ?></td>
            <td><?php echo $entry_sort_order; ?></td>
          </tr>
          <?php	$module_rows[$store['store_id']] = 0;
          $modules = explode(':', $store_array[$store['store_id']]['multiflat_cost']);
          $modules_count = count($modules);
          if($modules_count > 0) {
          	$store_array[$store['store_id']]['multiflat_cost'] = explode(':', $store_array[$store['store_id']]['multiflat_cost']);
          	$store_array[$store['store_id']]['multiflat_tax_class_id'] = explode(':', $store_array[$store['store_id']]['multiflat_tax_class_id']);
          	$store_array[$store['store_id']]['multiflat_geo_zone_id'] = explode(':', $store_array[$store['store_id']]['multiflat_geo_zone_id']);
          	$store_array[$store['store_id']]['multiflat_status'] = explode(':', $store_array[$store['store_id']]['multiflat_status']);
          	$store_array[$store['store_id']]['multiflat_sort_order'] = explode(':', $store_array[$store['store_id']]['multiflat_sort_order']);
          } ?>
          <?php for ($i = 0; $i < $modules_count; $i++) { ?>
          <tbody id="module-row<?php echo $store['store_id'] . '_' . $module_rows[$store['store_id']]; ?>">
            <tr>
            	<td>
              <input type="text" name="multiflat_cost_<?php echo $store['store_id'] . '_' . $i; ?>" value="<?php echo $store_array[$store['store_id']]['multiflat_cost'][$i]; ?>" /><?php $store_multiflat_total++; ?></td>
              
              <td><select name="multiflat_tax_class_id_<?php echo $store['store_id'] . '_' . $i;?>">
              <option value="0"><?php echo $text_none; ?></option>
              <?php foreach ($tax_classes as $tax_class) { ?>
              <?php if ($tax_class['tax_class_id'] == $store_array[$store['store_id']]['multiflat_tax_class_id'][$i]) { ?>
              <option value="<?php echo $tax_class['tax_class_id']; ?>" selected="selected"><?php echo $tax_class['title']; ?></option>
              <?php } else { ?>
              <option value="<?php echo $tax_class['tax_class_id']; ?>"><?php echo $tax_class['title']; ?></option>
              <?php } ?>
              <?php } ?>
              </select></td>
              
              <td><select name="multiflat_geo_zone_id_<?php echo $store['store_id'] . '_' . $i;?>">
              <option value="0"><?php echo $text_all_zones; ?></option>
              <?php foreach ($geo_zones as $geo_zone) { ?>
              <?php if ($geo_zone['geo_zone_id'] == $store_array[$store['store_id']]['multiflat_geo_zone_id'][$i]) { ?>
              <option value="<?php echo $geo_zone['geo_zone_id']; ?>" selected="selected"><?php echo $geo_zone['name']; ?></option>
              <?php } else { ?>
              <option value="<?php echo $geo_zone['geo_zone_id']; ?>"><?php echo $geo_zone['name']; ?></option>
              <?php } ?>
              <?php } ?>
              </select></td>
              
              <td><select name="multiflat_status_<?php echo $store['store_id'] . '_' . $i;?>">
              <?php if ($store_array[$store['store_id']]['multiflat_status'][$i]) { ?>
              <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
              <option value="0"><?php echo $text_disabled; ?></option>
              <?php } else { ?>
              <option value="1"><?php echo $text_enabled; ?></option>
              <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
              <?php } ?>
              </select></td>
              
              <td><input type="text" name="multiflat_sort_order_<?php echo $store['store_id'] . '_' . $i;?>" value="<?php echo  isset($store_array[$store['store_id']]['multiflat_sort_order'][$i]) ? $store_array[$store['store_id']]['multiflat_sort_order'][$i] : ''; ?>" size="1" /></td>
            <td class="right"><a onclick="$('#module-row<?php echo $store['store_id'] . '_' . $i; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
            </tr>
          </tbody>
          <?php $module_rows[$store['store_id']]++; ?>
          <?php } ?>
          <tfoot>
            <tr>
              <td colspan="6"></td>
              <td class="right"><a onclick="addModule(<?php echo $store['store_id']; ?>);" class="button"><?php echo $button_add_module; ?></a>
          		<input type="hidden" name="multiflat_<?php echo $store['store_id']; ?>" id="multiflat_<?php echo $store['store_id']; ?>" value="<?php echo $module_rows[$store['store_id']]; ?>" /></td>
            </tr>
          </tfoot>
        </table>
        <?php } ?>
      </form>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
var module_rows = new Array(<?php $values = ''; foreach($module_rows as $module) $values .= '"' . $module . '", '; echo trim($values, ', '); ?>);
function addModule(id) {
	html  = '<tbody id="module-row' + id + '_' + module_rows[id] + '">';
	html += '  <tr>';
	
	html += '    <td class="left"><input type="text" name="multiflat_cost_' + id + '_' + module_rows[id] + '" value="" /></td>';
	
	html += '    <td class="left"><select name="multiflat_tax_class_id_' + id + '_' + module_rows[id] + '">\
              <option value="0"><?php echo $text_none; ?></option>';
	<?php foreach ($tax_classes as $tax_class) { ?>
	html += '<option value="<?php echo $tax_class['tax_class_id']; ?>" ><?php echo $tax_class['title']; ?></option>';
  <?php } ?>
	html += '		 </select></td>';
	
	html += '    <td class="left"><select name="multiflat_geo_zone_id_' + id + '_' + module_rows[id] + '">\
              <option value="0"><?php echo $text_all_zones; ?></option>';
	<?php foreach ($geo_zones as $geo_zone) { ?>
	html += '<option value="<?php echo $geo_zone['geo_zone_id']; ?>" ><?php echo $geo_zone['name']; ?></option>';
  <?php } ?>
	html += '		 </select></td>';
	
	html += '    <td class="left"><select name="multiflat_status_' + id + '_' + module_rows[id] + '">\
              <option value="1"><?php echo $text_enabled; ?></option>\
              <option value="0"><?php echo $text_disabled; ?></option>\ ';
	html += '		 </select></td>';
	
	html += '    <td class="right"><input type="text" name="multiflat_sort_order_' + id + '_' + module_rows[id] + '" value="0" size="1" /></td>';
	html += '    <td class="right"><a onclick="$(\'#module-row' + id + '_' + module_rows[id] + '\').remove();" class="button"><?php echo $button_remove; ?></a></td>';
	html += '  </tr>';
	html += '</tbody>';
	
	$('#module' + id + ' tfoot').before(html);
	
	module_rows[id]++;
	$('#multiflat_' + id).val(module_rows[id]);
}
//--></script> 
<?php echo $footer; ?> 